﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Compiler
{
    class Shift : ParseTableAction
    {
        public SimpleParseTableState state;

        public Shift(SimpleParseTableState state)
        {
            this.state = state;
        }

        public void Execute(
            IEnumerator<LexerPair> sourceEnumerator, 
            Stack<Entry> stack,
            Stack<INode> nodeStack,
            INodeFactory nodeFactory)
        {
            LexerPair lexPair = sourceEnumerator.Current;

            Symbol topSymbol = lexPair.Token.Terminal;

            if (nodeFactory != null)
            {
                INode node = nodeFactory.OnTerminal(lexPair);
                if (node != null)
                    nodeStack.Push(node);
            }

            stack.Push(new Entry(state, topSymbol));
            sourceEnumerator.MoveNext();
        }

        public override string ToString()
        {
            return "s" + state.ToString();
        }
    }
}
